package com.inventory.api.entity;

import com.inventory.api.signUpReq.InvntSignupReq;
import com.inventory.api.signUpReq.StockCheckSignUpReq;
import com.inventory.api.util.UuidUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.*;
import java.util.Date;

/**
 * Created by lihu on 2018/2/1.
 */
@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
public class StockTransaction {

    @Id
    @Column(length = 50)
    private String id;
    private String productId;
    private String productName;
    private String storeId;

    private int stockPrice;
    @Enumerated(EnumType.STRING)
    private StockType type;

    @Enumerated(EnumType.STRING)
    private StockSource source;
    /**
     * 关联Id, 正常入库就关联入库ID，盘点入库就关联盘点id
     */
    private String refId;
    /**
     * 操作人
     */
    private String operatorId;

    /**
     * 操作人
     */
    private String operatorName;
    /**
     * 备注
     */
    private String notes;


    /**
     * 个
     */
    private int single = 0;

    /**
     * 件
     */
    private int whole = 0;

    /**
     * 规格
     */
    private int stockQuantityRatio = 1;
    /**
     * 操作时间
     */
    @Column(columnDefinition = "TIMESTAMP(3) NULL")
    private Date operTime;
    @Column(insertable = false, updatable = false, columnDefinition = "TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP(3)")
    private Date addTime;


    /**
     * 出入库数据初始化
     *
     * @param inventore
     * @param storeId
     * @param type
     * @param source
     * @param id
     * @param operatorId
     * @param operatorName
     * @param notes
     */
    public StockTransaction(InvntSignupReq.Inventore inventore, String storeId,
                            StockType type, StockSource source, String id,
                            String operatorId, String operatorName, String notes) {
        this.id = UuidUtil.getUuid();
        this.productId = inventore.getProductId();
        this.productName = inventore.getProductName();
        this.storeId = storeId;
        this.stockPrice = inventore.getStockPrice();
        this.type = type;
        this.source = source;
        this.refId = id;
        this.operatorId = operatorId;
        this.operatorName = operatorName;
        this.operTime = new Date();
        this.notes = notes;
        this.stockQuantityRatio = inventore.getStockQuantityRatio();
        single = inventore.getSingle();
        whole = inventore.getWhole();
    }


    /**
     * 盘点出入库记录
     *
     * @param checkProduct
     * @param operatorId
     * @param operatorName
     * @param storeId
     * @param stockPrice
     * @param orderId
     */
    public StockTransaction(StockCheckSignUpReq.CheckProduct checkProduct,
                            String operatorId, String operatorName, String storeId,
                            int stockPrice, String orderId) {
        id = UuidUtil.getUuid();
        productId = checkProduct.getProductId();
        productName = checkProduct.getProductName();
        this.storeId = storeId;
        this.stockPrice = stockPrice;
        if (checkProduct.getCheckTotalQty() >
                checkProduct.getAccurateTotalQty()) {
            type = StockType.STOCK_IN;
            source = StockSource.CHECK_IN;
        } else {
            type = StockType.STOCK_OUT;
            source = StockSource.CHECK_OUT;
        }
        this.refId = orderId;
        this.operatorId = operatorId;
        this.operatorName = operatorName;
        this.operTime = new Date();
        this.notes = checkProduct.getNotes();
        stockQuantityRatio = checkProduct.getStockQuantityRatio();
        single = Math.abs(checkProduct.getAccurateSingleQty() - checkProduct.getCheckSingleQty());
        whole = Math.abs(checkProduct.getAccurateWholeQty() - checkProduct.getCheckWholeQty());
    }

    /**
     * 商品盘点出库记录
     *
     * @param checkProduct
     * @param operatorId
     * @param operatorName
     * @param storeId
     * @param stockPrice
     * @param orderId
     */
    public StockTransaction StockIn(StockCheckSignUpReq.CheckProduct checkProduct,
                                    String operatorId, String operatorName, String storeId,
                                    int stockPrice, String orderId) {
        id = UuidUtil.getUuid();
        productId = checkProduct.getProductId();
        productName = checkProduct.getProductName();
        this.storeId = storeId;
        this.stockPrice = stockPrice;
        type = StockType.STOCK_IN;
        source = StockSource.CHECK_IN;
        this.refId = orderId;
        this.operatorId = operatorId;
        this.operatorName = operatorName;
        this.operTime = new Date();
        this.notes = checkProduct.getNotes();
        stockQuantityRatio = checkProduct.getStockQuantityRatio();
        /**
         * 应有数量比实际数量小入库
         */
        if (checkProduct.getAccurateSingleQty() - checkProduct.getCheckSingleQty() < 0) {
            single = checkProduct.getAccurateSingleQty() - checkProduct.getCheckSingleQty();
        } else {
            whole = checkProduct.getAccurateWholeQty() - checkProduct.getCheckWholeQty();
        }
        return this;
    }

    /**
     * 商品盘点入库记录
     *
     * @param checkProduct
     * @param operatorId
     * @param operatorName
     * @param storeId
     * @param stockPrice
     * @param orderId
     */
    public StockTransaction StockOut(StockCheckSignUpReq.CheckProduct checkProduct,
                                     String operatorId, String operatorName, String storeId,
                                     int stockPrice, String orderId) {
        id = UuidUtil.getUuid();
        productId = checkProduct.getProductId();
        productName = checkProduct.getProductName();
        this.storeId = storeId;
        this.stockPrice = stockPrice;
        type = StockType.STOCK_OUT;
        source = StockSource.CHECK_OUT;
        this.refId = orderId;
        this.operatorId = operatorId;
        this.operatorName = operatorName;
        this.operTime = new Date();
        this.notes = checkProduct.getNotes();
        stockQuantityRatio = checkProduct.getStockQuantityRatio();
        /**
         * 应有数量比实际数量大出库
         */
        if (checkProduct.getAccurateSingleQty() - checkProduct.getCheckSingleQty() > 0) {
            single = checkProduct.getCheckSingleQty() - checkProduct.getAccurateSingleQty();
        } else {
            whole = checkProduct.getCheckWholeQty() - checkProduct.getAccurateWholeQty();
        }
        return this;
    }

}
